package server

import (
	"encoding/json"

	"go.uber.org/zap"
)

// Novels novels struct
type Novels struct {
	NovelsSer *NovelsSer
}

var gNovels *Novels

// New new
func New() *Novels {
	return &Novels{}
}

// Start start
func (novels *Novels) Start() error {
	Logger.Info("Start", zap.Any("conf", Conf))
	// 加载需要拉取的小说网站地址
	if err := novels.initNovelsAddrs(); err != nil {
		Logger.Panic("initSearchSerMsg", zap.Error(err))
	}
	gNovels = novels

	// start web server
	go webServer()

	return nil
}

func (novels *Novels) initNovelsAddrs() error {

	serConf := newNovelsSer() //&NovelsSer{}
	err := json.Unmarshal([]byte(Conf.NovelsC.NovelsAddr), serConf)
	if err != nil {
		Logger.Error("json", zap.Error(err))
		return err
	}
	novels.NovelsSer = serConf
	// log.Println(serConf)

	return nil
}

// Stop stop
func (novels *Novels) Stop() error {
	return nil
}
